﻿using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Wedn.Net.Common;
using Wedn.Net.Utility;
namespace Wedn.Net.Model
{
    public partial class Post
    {
        /// <summary>
        /// 链接
        /// </summary>
        public string Link { get; set; }
        /// <summary>
        /// 作者
        /// </summary>
        public User Author { get; set; }
        /// <summary>
        /// 类别
        /// </summary>
        public IEnumerable<Term> Categorys { get; set; }
        /// <summary>
        /// 标签
        /// </summary>
        public IEnumerable<Term> Tags { get; set; }
        /// <summary>
        /// 评论
        /// </summary>
        public IEnumerable<Comment> Comments { get; set; }
        /// <summary>
        /// 元数据
        /// </summary>
        public IDictionary<string, string> Meta { get; set; }
        /// <summary>
        /// 前后文章
        /// </summary>
        public Around<Post> Around { get; set; }
        
        #region 文章缩略图 +string Image(string def)
        /// <summary>
        /// 文章缩略图
        /// </summary>
        public string Image(string def)
        {
            Match match = Regex.Match(this.Content, "<img.+?src=\"(.+?)\".+?>");
            if (match.Length > 0)
                def = match.Groups[1].Value;
            return def;
        } 
        #endregion

        #region 文章概要 +string Note(int length)
        /// <summary>
        /// 文章概要
        /// </summary>
        /// <param name="length">最大长度</param>
        /// <returns>文章概要</returns>
        public string Note(int length)
        {
            return Note(length, "...");
        } 
        #endregion

        #region 文章概要 string Note(int length, string moreFlag)
        /// <summary>
        /// 文章概要
        /// </summary>
        /// <param name="length">最大长度</param>
        /// <param name="moreFlag">更多标识</param>
        /// <returns>文章概要</returns>
        public string Note(int length, string moreFlag)
        {
            if (!string.IsNullOrEmpty(Excerpt)) return Excerpt;
            var temp = Content.TrimHtml();
            return temp.RealLength() > length ? StringHelper.CutString(temp, length) + moreFlag : temp;
        } 
        #endregion

        #region 元拓展信息数据值 +string MetaValue(string key)
        /// <summary>
        /// 元拓展信息数据值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string MetaValue(string key)
        {
            return Meta.ContainsKey(key) ? Meta[key] : string.Empty;
        } 
        #endregion

        #region 主题下元拓展信息数据值 +string ThemeMetaValue(string key)
        /// <summary>
        /// 主题下元拓展信息数据值
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string ThemeMetaValue(string key)
        {
            key = Setting.GetThemeOptionKey(key);
            return Meta.ContainsKey(key) ? Meta[key] : string.Empty;
        } 
        #endregion
    }
}
